import babel from 'gulp-babel'
import sourcemaps from 'gulp-sourcemaps'
import gulp from 'gulp'
import eslint from 'gulp-eslint'

// 配置需要处理的文件目录和转码之后文件的存放目录
const paramConfig = {
    source: ['src/**/*.js', 'lib/**/*.js'],
    dest: 'dist',
}

gulp.task('lint', () => {
    // eslint配置，使用配置的文件目录。排除node_modules下的全部文件。
    return gulp.src([...paramConfig.source, '!node_modules/**'])
        .pipe(eslint())
        .pipe(eslint.result(result => {
            console.log(`ESLint result: ${result.filePath}`);
            console.log(`# Messages: ${result.messages.length}`);
            console.log(`# Warnings: ${result.warningCount}`);
            console.log(`# Errors: ${result.errorCount}`);
        }))
        .pipe(eslint.format())
        .pipe(eslint.failOnError())
});

gulp.task('default', ['lint', 'babel-sourcemaps'], () => {
    console.log('gulp default task!')
});

gulp.task('babel-sourcemaps', () => {
    return gulp.src(paramConfig.source)
        .pipe(sourcemaps.init())
        .pipe(babel())
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(paramConfig.dest))
});

gulp.task('babel', () => {
    return gulp.src(paramConfig.source)
        .pipe(babel())
        .pipe(gulp.dest(paramConfig.dest))
});